#include <stdio.h>
#include <stdlib.h>
//单链表的应用——反转链表
typedef int ElemType;
typedef struct node
{
    ElemType data;
    struct node *next;
}Node;
Node *Initlist()
{
    Node *s=(Node *)malloc(sizeof(Node));
    s->data=0;
    s->next=NULL;
    return s;
}
//尾插法
void addtail(Node *ps,ElemType n)
{
    Node*p=ps;
    while(p->next)
    {
        p=p->next;
    }
    Node * q=(Node*)malloc(sizeof(Node));
    q->data=n;
    p->next=q;
    q->next=NULL;
}
void printlist(Node *ps)
{
    Node *p=ps->next;
    while(p)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}
//反转链表
void fanzhuanlist(Node *ps)
{
    Node * a,*b,*c;
    a=NULL;
    b=ps->next;
    while(b)
    {
        c=b->next;
        b->next=a;
        a=b;
        b=c;
    }
    b=ps;
    b->next=a;
}
//释放链表(头节点也被释放)
void freelist(Node *ps)
{
    Node *p=ps;
    Node *q;
    while(p)
    {
        q=p->next;
        free(p);
        p=q;
    }
}
int main()
{
    Node *list=Initlist();
    addtail(list,1);
    addtail(list,2);
    addtail(list,3);
    addtail(list,4);
    addtail(list,5);
    addtail(list,6);
    printlist(list);
    //反转
    fanzhuanlist(list);
    printlist(list);
    freelist(list);
    return 0;
}